﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using HadronData;

namespace Hadron
{
    class GameStateLose
        : GameState
    {
        private enum FadeState
        {
            FadeIn,
            Running,
            FadeOut
        }

        private static GameState s_gameState = new GameStateLose();

        private FadeState m_fadeState;
        private float m_fStateTimer;
        private Texture2D m_textureBackground;
        private Texture2D m_textureGamepadStart;
        private GameFont m_gameFont;

        public static GameState Instance
        {
            get { return s_gameState; }
        }

        private GameStateLose()
        {
        }

        public override void OnEnter()
        {
            ContentManager contentManager = m_gameContext.Game.Content;

            m_textureBackground = contentManager.Load<Texture2D>("Graphics\\LoseBackground");
            m_textureGamepadStart = contentManager.Load<Texture2D>("Graphics\\GamepadStart");
            m_gameFont = new GameFont(AssetManager.Fonts.Title);

            m_fadeState = FadeState.FadeIn;
            m_fStateTimer = 0.5f;
        }

        public override void OnExit()
        {
            m_gameFont = null;

            m_textureGamepadStart = null;
            m_textureBackground = null;
        }

        public override void Update(GameTime p_gameTime)
        {
            float fDeltaTime = (float)p_gameTime.ElapsedGameTime.TotalSeconds;

            if (m_fadeState == FadeState.Running && m_gameController.Start)
            {
                m_fStateTimer = 0.5f;
                m_fadeState = FadeState.FadeOut;
                AssetManager.Sounds.MenuItem.Play();
            }

            m_fStateTimer = Math.Max(0.0f, m_fStateTimer - fDeltaTime);

            if (m_fStateTimer == 0.0f)
            {
                switch (m_fadeState)
                {
                    case FadeState.FadeIn: m_fadeState = FadeState.Running; break;
                    case FadeState.FadeOut:
                        ScoreTable scoreTable = new ScoreTable();
                        scoreTable.Load(m_gameContext.StorageDevice);
                        if (m_gameContext.Score > 0
                            && scoreTable.Qualifies(m_gameContext.Score))
                            NextState = GameStateScoreName.Instance;
                        else
                            NextState = GameStateScores.Instance;
                        break;
                }
            }
        }

        public override void Draw(SpriteBatch p_spriteBatch)
        {
            p_spriteBatch.Begin();
            p_spriteBatch.Draw(m_textureBackground, Vector2.Zero, Color.White);

            m_gameFont.Colour = Color.YellowGreen;
            m_gameFont.Alignment = GameFontAlignment.Centre;
            m_gameFont.DrawString(p_spriteBatch, new Vector2(400.0f, 300.0f),
                "Game Over");

            float fFadeOpacity = 0.0f;
            switch (m_fadeState)
            {
                case FadeState.FadeIn: fFadeOpacity = m_fStateTimer * 2.0f; break;
                case FadeState.Running: fFadeOpacity = 0.0f; break;
                case FadeState.FadeOut: fFadeOpacity = 1.0f - m_fStateTimer * 2.0f; break;
            }
            
            // mission buttons
            p_spriteBatch.Draw(m_textureGamepadStart, new Vector2(600.0f, 500.0f), Color.White);
            m_gameFont.Alignment = GameFontAlignment.Left;
            m_gameFont.Colour = Color.LightGray;
            m_gameFont.DrawString(p_spriteBatch, new Vector2(640.0f, 508.0f), "Main menu");

            // fade in/out
            p_spriteBatch.Draw(AssetManager.Textures.Fade,
                new Rectangle(0, 0, 800, 600),
                new Color(Color.Black, fFadeOpacity));

            p_spriteBatch.End();
        }
    }
}
